/*
Copyright (C) 2009  Raymond Jeon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/






#include "gl_mail_mainparent.h"
#include "gl_mail_define.h"
#include "gl_mail_gdata.h"
#include "gl_lib_include.h"
#include "gl_mail_log.h"





////////////////////////////////////////////////////////////////////////////////
//	Date : 2003-2-20
//	Update : 
//	Author : Raymond Jeon
//	Parameters
//	Return Value  
//	Remarks 
////////////////////////////////////////////////////////////////////////////////

static int GlCreateChildProcess(HANDLE *handle, int argc, char **argv)
{
	int rv, i;
	char buf[1024];
	char szPath[1024];
    STARTUPINFO si;           
    PROCESS_INFORMATION pi;   
    SECURITY_ATTRIBUTES sa = {0};  


	rv = GetModuleFileName(NULL, buf, sizeof(buf));

    if (rv == sizeof(buf))
	{
        return -1;
    } 
	else if (rv == 0) 
	{
        return -1;
    }

    sprintf(szPath, "\"%s\" -Z", buf);  

    for (i = 1; i < argc; i++) 
	{
		GlSprintf(szPath + strlen(szPath), 1024 - strlen(szPath), " \"%s\"", argv[i]);  
    }

	memset(&si, 0, sizeof(si));
    memset(&pi, 0, sizeof(pi));
    si.cb = sizeof(si);
    si.dwFlags     = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    si.wShowWindow = SW_HIDE;
	
	GLTRACE("CreateProcess(%s)", szPath);
    if (!CreateProcess(NULL, szPath, NULL, NULL, 
                       TRUE,   
                       0,      
                       NULL, NULL,
                       &si, &pi)) 
	{

		GLTRACE("CreateProcess error(%d)", GetLastError());
		GLETRACE("CreateProcess error(%d)", GetLastError());

        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
		return -1;
    }

	*handle = pi.hProcess;
	CloseHandle(pi.hThread);

	return 0;

}





////////////////////////////////////////////////////////////////////////////////
//	Date : 2003-2-20
//	Update : 
//	Author : Raymond Jeon
//	Parameters
//	Return Value  
//	Remarks 
////////////////////////////////////////////////////////////////////////////////

int GlParentMain(int argc, char *argv[]) 
{
	int iRet;

	GLTRACE("@@@@@ Create parent process @@@@@");
	GlLogAll(1, "@@@@@ Create parent process @@@@@");
	

	GlCheckDefaultFiles();


	do 
	{
		
		if (glData.bStopSuspending) 
		{
			GLTRACE("glData.bStopSuspending");
			break;
		}
		
		GlServiceSetStatus(SERVICE_START_PENDING);
		if (GlCreateChildProcess(&(glData.hChildProcess), argc, argv)) 
		{
			GLTRACE("GlCreateChildProcess error");
			break;
		}
		
		GLTRACE("SERVICE_RUNNING");
		GlServiceSetStatus(SERVICE_RUNNING);
		glData.bStopSuspending = 0;

        iRet = WaitForMultipleObjects(1, (HANDLE *)(&(glData.hChildProcess)), FALSE, INFINITE);

		GLTRACE("@@@@@ child process dead @@@@@");
		GLETRACE("@@@@@ child process dead @@@@@");
		GlLogAll(1, "@@@@@ child process dead @@@@@");

        if (iRet == WAIT_FAILED) 
		{
			GLTRACE("@@@@@ WAIT_FAILED @@@@@");
			GLETRACE("@@@@@ WAIT_FAILED @@@@@");
			GlLogAll(1, "@@@@@ WAIT_FAILED @@@@@");
			glData.bStopSuspending = TRUE;
			break;
		}
        if (iRet == WAIT_TIMEOUT) 
		{
			GLTRACE("@@@@@ WAIT_TIMEOUT @@@@@");
			GLETRACE("@@@@@ WAIT_TIMEOUT @@@@@");
			GlLogAll(1, "@@@@@ WAIT_TIMEOUT @@@@@");
            glData.bStopSuspending = TRUE;
		} 
		else 
		{
		}


	} while (glData.bStopSuspending == FALSE);


	GLTRACE("SERVICE_STOPPED");
	GlServiceSetStatus(SERVICE_STOPPED);

	return TRUE;
}



#define DEFAULT_INSTALL_BAT		"gmail.exe -i"

#define DEFAULT_UNINSTALL_BAT	"gmail.exe -u"




static void GlCheckDefaultFile(char *szFileName, char *szData)
{
	char szFile[_MAXPATH + 1];

	GlSprintf(szFile, _MAXPATH, "%s\\%s", glData.szDirCurWork, szFileName);

	if (!GlFileExist(szFile))
		GlFileSetString(szFile, szData);
}

void GlCheckDefaultFiles()
{
		
//	GlCheckDefaultFile("serviceinstall.bat", DEFAULT_INSTALL_BAT);
//	GlCheckDefaultFile("serviceuninstall.bat", DEFAULT_UNINSTALL_BAT);
}


